using UnityEngine;
using System.Collections;

public class CharacterData
{
    public AttributeSet attributes;
    public Perks.PerkSet perks;
    public Inventory inventory;
	
	public Item[] hotSkills;

    public bool assigned = false;
    private string characterName = "";
    public CharacterData(string name = "")
    {
        attributes = new AttributeSet(1,1,1,1,1,10);
        perks = new Perks.PerkSet(attributes);
		hotSkills = new Item[3];
		characterName = name;
		ResetHotSkills();
    }
	public void CreateInventory()
    {
        int cols = 10;
        int rows = 7;
        if (perks.IsEquipped(Perks.BackpackIncrease.Name))
        {
            rows += 3;
        }
        if (perks.IsEquipped(Perks.BackpackIncreasePlus.Name))
        {
            rows += 3;
        }
        inventory = new Inventory(rows, cols);
    }
	public void RefillValues(){
		this.attributes.RefillValues();	
	}
    public void Save()
    {
        Debug.Log("Saving Character: " + characterName);
        CharacterInfo character = new CharacterInfo(attributes, perks);
        SaveDataManager.GetInstance().SaveFile((object)character, characterName);
    }
    public void Load(string charName)
    {
		characterName = charName;
		
        CharacterInfo character =
            SaveDataManager.GetInstance().LoadFile<CharacterInfo>(characterName);
		
		//Seteo los valores de los atributos con la info que cargue.
		attributes.SetAttributeDataArray(ATTRIBUTES.AGILITY, 
			character.GetAttributeData(ATTRIBUTES.AGILITY) );
		attributes.SetAttributeDataArray(ATTRIBUTES.CONSTITUTION, 
			character.GetAttributeData(ATTRIBUTES.CONSTITUTION) );
		attributes.SetAttributeDataArray(ATTRIBUTES.DEXTERITY, 
			character.GetAttributeData(ATTRIBUTES.DEXTERITY) );
		attributes.SetAttributeDataArray(ATTRIBUTES.STRENGTH, 
			character.GetAttributeData(ATTRIBUTES.STRENGTH) );
		attributes.SetAttributeDataArray(ATTRIBUTES.TECHNIQUE, 
			character.GetAttributeData(ATTRIBUTES.TECHNIQUE) );
		attributes.SetAttributePoints(character.GetAttributePoints() );
		
		//Seteo los valores de los sub-atributos con la info que cargue.
		attributes.SetSubAttributeDataArray(SUB_ATTRIBUTES.HIT_POINTS,
			character.GetSubAttributeData(SUB_ATTRIBUTES.HIT_POINTS) );
		attributes.SetSubAttributeDataArray(SUB_ATTRIBUTES.INVENTORY_SLOTS,
			character.GetSubAttributeData(SUB_ATTRIBUTES.INVENTORY_SLOTS) );
		attributes.SetSubAttributeDataArray(SUB_ATTRIBUTES.MOVEMENT_SPEED,
			character.GetSubAttributeData(SUB_ATTRIBUTES.MOVEMENT_SPEED) );
		attributes.SetSubAttributeDataArray(SUB_ATTRIBUTES.NOISE_FACTOR,
			character.GetSubAttributeData(SUB_ATTRIBUTES.NOISE_FACTOR) );
		attributes.SetSubAttributeDataArray(SUB_ATTRIBUTES.EQUIP_TIME,
			character.GetSubAttributeData(SUB_ATTRIBUTES.EQUIP_TIME) );
		attributes.SetSubAttributeDataArray(SUB_ATTRIBUTES.STAMINA,
			character.GetSubAttributeData(SUB_ATTRIBUTES.STAMINA) );
		//Ahora equipo los perks
		perks.UnEquipAllPerks();
		string[] equippedPerks = character.GetEquippedPerks();
		foreach(string perkName in equippedPerks){
			perks.Equip(perkName);
		}
		//Actualizo la visual para mostrar bien todos los datos.
		AttributesController.GetInstance().UpdateStats();
    }
    public void SetName(string name)
    {
        characterName = name;
        AttributesController.GetInstance().UpdateStats();
    }
    public string GetName(){return this.characterName;}
    public void UseHotSkill(int index)
    {
        if (hotSkills[index] != null)
        {
            hotSkills[index].Use();
        }
    }
    /// <summary>
    /// Se fija si un item esta en los hotkeys y lo saca si esta.
    /// </summary>
    /// <param name="item"></param>
    public void RemoveHotSkill(Item item)
    {
        for (int i = 0; i < hotSkills.Length; i++)
        {
            if (hotSkills[i] == item)
            {
                hotSkills[i] = null;
            }
        }
    }
    public void ResetHotSkills()
    {
        for (int i = 0; i < hotSkills.Length; i++)
        {
            hotSkills[i] = null;
        }
    }
}
